package com.ruoyi.pg.evaluate.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pg.evaluate.vo.PgAssessmentVo;
import com.ruoyi.pg.evaluate.service.IPgAssessmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 评估Controller
 *
 * @author Ming
 * @date 2025-08-22
 */
@Api(tags = "评估Controller")
@RestController
@RequestMapping("/evaluate/assessment")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PgAssessmentController extends BaseController {

    private final IPgAssessmentService pgAssessmentService;

    @ApiOperation("查询评估列表")
    @GetMapping("/list")
    public TableDataInfo<PgAssessmentVo> list(PgAssessmentVo entity) {
        return pgAssessmentService.queryList(entity);
    }

    @ApiOperation("查询评估所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(PgAssessmentVo entity) {
        return AjaxResult.success("查询成功", pgAssessmentService.queryAll(entity));
    }

    @ApiOperation("导出评估列表")
    @Log(title = "评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PgAssessmentVo entity) {
        List<PgAssessmentVo> list = pgAssessmentService.queryAll(entity);
        ExcelUtil<PgAssessmentVo> util = new ExcelUtil<>(PgAssessmentVo.class);
        util.exportExcel(response, list, "评估数据");
    }

    @ApiOperation("获取评估详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", pgAssessmentService.queryById(id));
    }

    @ApiOperation("新增评估")
    @Log(title = "评估", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PgAssessmentVo entity) {
        return toAjax(pgAssessmentService.insertAssessment(entity));
    }

    @ApiOperation("修改评估")
    @Log(title = "评估", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PgAssessmentVo entity) {
        return toAjax(pgAssessmentService.updateById(entity));
    }

    @ApiOperation("删除评估")
    @Log(title = "评估", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pgAssessmentService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("导出能力等级评估报告")
    @PostMapping("exportToWord")
    public AjaxResult exportToWord(@RequestBody PgAssessmentVo entity) {
        pgAssessmentService.exportToWord(entity);
        return AjaxResult.success("导出成功");
    }
}
